var STB = function(){
    var that = this;
    this.repo = null;
    this.init = function(){
        this.repo = new STB_Repo();
        this.views = {
            billStart: new billStart_view(),
            startPage: new start_view(),
            showBill: new showBill_view(),
            billsArchive: new billsArchive_view(),
            addPerson: new addPerson_view(),
            addItem: new addItem_view()
        };
    };
    
    this.getViewHandler = function(viewId){
        return this.views[viewId];
    };
//Bill functions    
    this.showSelectedBill = function(){
        var billDrawer = that.getViewHandler('showBill');
        billDrawer.showBill(this.repo.bill);
    };

    this.setBillToShow = function(ident, isLocal){
        
        this.repo.findBill(ident, isLocal, that.setBillToShowSuccess, that.setBillToShowFail);
    };
    
    this.setBillToShowSuccess = function(Bill){
        that.setCurrentBill(Bill);
        $.mobile.changePage( "show_bill.html", {'billToShow': Bill} );
    };
    
    this.setBillToShowFail = function(args, res){
        debugger;  
    };
    this.showLatestBill = function(){
        this.repo.findCurrentBill(
            function(Bill){
                that.setCurrentBill(Bill);
                var startPageDrawer = that.getViewHandler('startPage');
                startPageDrawer.renderLatestBill(Bill);
            }, 
            function(args){debugger;
        });
    };
    
    this.setCurrentBill = function(Bill, callBack){
        this.repo.setCurrentBill(Bill);
        if(typeof callBack != 'undefined'){
            callBack.call(this, Bill);
        }
    };    
    
    this.addBill = function(name, callBack){
        if(typeof name != 'undefined' && name != ''){
            this.repo.makeNewBill(name, that.addBillSuccess, that.addBillFail);
        }else{
            this.addBillSuccess();
        }
    };
    
    this.addBillSuccess = function(Bill){
        that.repo.setCurrentBill(Bill);
        $.mobile.changePage( "add_person.html", {} );
    };
    
    this.addBillFail = function(args, success){
        debugger;
    };
    this.getStartPageBills = function(){
        this.repo.getBillsWithLimit(
            3,
            function(billsList){
                var billStartDrawer = that.getViewHandler('billStart');
                billStartDrawer.renderBillsList(billsList);
            }, 
            this.getBillsFail
        );
    };
    this.getBills = function(){
        this.repo.getBills(
            function(billsList){
                var billsArchiveDrawer = that.getViewHandler('billsArchive');
                billsArchiveDrawer.renderBillsList(billsList);
            }, 
            that.getBillsFail
        );
    };    
    
    this.getBillsSuccess = function(billsList, callBack){
        callBack.call(this, billsList);
    };
    
    
    this.getBillsFail = function(args, callBack){
        debugger;
    };
//person functions
    this.addPersonFormShow = function(){
        this.repo.getPeople(
            that.repo.bill.getDbIdent(), 
            function(personsList){
                var addPersonDrawer = that.getViewHandler('addPerson');
                addPersonDrawer.addPeopleSelector(personsList); 
            }, 
            this.getPeopleSelectorFail
        );
    };
    this.addPerson = function(peopleArr){
        if(peopleArr.length > 0){
            for(var i=0;i<peopleArr.length;i++){
                var personVal = peopleArr[i];
                //TODO: make it possible to add from contacts list
                if(personVal.value != 'submit-value'){
                    var currBillIdent = that.repo.bill.getDbIdent();
                    this.repo.addPerson(personVal.value, currBillIdent, that.addPersonSuccess, that.addPersonFail);
                }
            }
        }
    };
    
    this.addPersonField = function(){
        var addPersonDrawer = that.getViewHandler('addPerson');
        addPersonDrawer.addpersonField();
    };
    
    this.addPersonSuccess = function(Person){
        $.mobile.changePage( "show_bill.html", {} );
    };
    
    this.addPersonFail = function(args, res){
        debugger;  
    };
    
//item functions
    this.getPeopleSelector = function(){
        this.repo.getPeople(
            that.repo.bill.getDbIdent(), 
            function(personsList){
                var addItemDrawer = that.getViewHandler('addItem');
                addItemDrawer.addPeopleSelector(personsList); 
            }, 
            this.getPeopleSelectorFail
        );
    };
    this.getPeopleSelectorFail = function(args, res){
        debugger;  
    };
    this.addItemWithSharers = function(itemInfoArr, personsInfoArr){
        if(itemInfoArr.length > 0){
            var itemName = '';
            var itemPrice = '';
            var itemQty = '';
            for(var i=0;i<itemInfoArr.length;i++){
                var itemVal = itemInfoArr[i];
                
                switch(itemVal.name){
                    case 'item-price':
                        itemPrice = itemVal.value;
                        break;
                    case 'item-name':
                        itemName = itemVal.value;
                        break;
                    case 'item-qty':
                        itemQty = itemVal.value;
                        break;
                }
            }
            that.repo.addItem(itemName, that.repo.bill.getDbIdent(), itemPrice, itemQty, that.addItemSuccess, that.addItemFail);
            if(personsInfoArr.length > 0){
                for(var x=0;x<personsInfoArr.length;x++){
                    var personVal = personsInfoArr[x];
                }
            }
        }
        
    }
    
    this.addItemSuccess = function(Item){
        debugger;
    };
    
    this.addItemFail = function(args, result){
        
    };
//parang constructor
    this.init();  
};

STB.prototype = new Engine;

var AppInstance = new STB();

